<template>
  <!-- 弹出新增窗口 
        title 窗口的标题
        :visible.sync 当它true的时候，窗口会被弹出
        -->
  <el-dialog
    :title="pojo.id == null ? '新增供应商' : '修改供应商'"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    width="500px"
  >
    <el-form
      :rules="rules"
      ref="pojoForm"
      label-width="100px"
      label-position="right"
      style="width: 400px"
      :model="pojo"
    >
      <el-form-item label="供应商名称" prop="name">
        <el-input v-model="pojo.name"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="linkman">
        <el-input v-model="pojo.linkman"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input v-model="pojo.mobile"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="pojo.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm('pojoForm')"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import supplierApi from "@/api/supplier";
export default {
  data() {
    return {
      rules: {
        // 校验规则
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        linkman: [
          { required: true, message: "联系人不能为空", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "电话号码不能为空", trigger: "blur" },
        ],
      },
    };
  },

  props: ["dialogVisible", "pojo", "closeDialog"],

  components: {},

  methods: {
    // 提交新增数据
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //提交表单
          this.addOrUpdate();
        }
      });
    },

    async addOrUpdate() {
      const { data } = await supplierApi.add(this.pojo);
      this.$message("成功");
      //新增成功，刷新列表数据
      this.handleClose(); // 关闭窗口
    },

    handleClose() {
      this.$nextTick(() => {
        this.$refs["pojoForm"].resetFields();
        this.closeDialog();
      });
    },
  },
};
</script>

<style scoped>
</style>